##z = matrix(r)
inst <-function(n,k=0)
{
x=rnorm(n)
y=rnorm(n)
plot(x,y)
drawcompletegraph(x,y,start=FALSE,lty=3)
s=c(1:n)
t=c(1:k)	
if(k==0)
{
	for(k in (n-1) )
##	t=c(1:k)	
	subset(x,y,s,n,k,t)
}
else
{
	subset(x,y,s,n,k,t)
}

}

isconvex <-function(x,y,t)
{

len=length(x[t]);
hul=chull(x[t],y[t]);
sz=length(hul);

if(sz==len)
{
polygon(x[t[hul]],y[t[hul]],border="red",lwd=2)
#points(x[hul],y[hul],pch=",",col="red")
return (TRUE);
}
else
{
return (FALSE);
}
}

subset <- function(x,y,s,p,k,t, q=0, r=0)
{
    if(q==k)
    {
        #for(i in (1:(k)) )
        {
        	#print("r :");
        	#print(r);
        	#print(t[1:k]);
        	#print(z[t[1:k]])
        	if(isconvex(x,y,t[1:k])==TRUE)
        	{
        		#plot(x[t[1:k]],y[t[1:k]]);
        		#id=chull(x[t[1:k]],y[t[1:k]]);
        		#polygon(x[id],y[id]);
			return (TRUE);
        	}
        	else
        	{
        		return (FALSE);
        	
        	}
        	#write((z[i]),'"|cmd"')
        }
        
    }

    else
    {
	    if(r<p)
	    {
		for(i in ( (r):(p-1)) )
		{
		    t[q+1]=s[i+1];
		    if(subset(x,y,s,p,k,t,q+1,i+1)==TRUE)
		    {
		    	return (TRUE);
		    }
		}
	    }
	    return (FALSE);
    }
}

